//
//  DietDetailViewController.m
//  muscleLife
//
//  Created by xalo on 16/4/29.
//  Copyright © 2016年 HeavenAndHell. All rights reserved.
//

#import "DietDetailViewController.h"
#import "RecipeViewController.h"
#import "DietDetailTableViewCell.h"
#import <MobAPI/MobAPI.h>
@interface DietDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSMutableArray *dataSource;//存储数据的数组
@end

@implementation DietDetailViewController

//数据源的懒加载
-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource=[NSMutableArray array];
    }
    return _dataSource;
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:self.view.frame];
        _tableView.delegate=self;
        _tableView.dataSource=self;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    [self acquireDataSource];
    [self.tableView registerNib:[UINib nibWithNibName:@"DietDetailTableViewCell" bundle:nil] forCellReuseIdentifier:@"DietDetailTableViewCell"];
    
}

#pragma mark---TableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DietDetailTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"DietDetailTableViewCell" forIndexPath:indexPath];
    ListModel *model=self.dataSource[indexPath.row];
    [cell.thumbnailImage sd_setImageWithURL:[NSURL URLWithString:model.thumbnail]];
    cell.nameLabel.text=model.name;
    cell.ctgTitlesLabel.text=model.ctgTitles;
    return cell;
}

#pragma mark---TableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RecipeViewController *recipeVC=[[RecipeViewController alloc]init];
    ListModel *model=self.dataSource[indexPath.row];
    recipeVC.model=model;
    recipeVC.menuId=model.menuId;
    [self.navigationController pushViewController:recipeVC animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 130;
}

//请求数据
-(void)acquireDataSource{
    [MobAPI sendRequest:[MOBACookRequest searchMenuRequestByCid:self.model.ctgId name:nil page:1 size:20] onResult:^(MOBAResponse *response) {
        if (response.error) {
            NSLog(@"error=%@",response.error);
        }else{
            NSArray *listArray=response.responder[@"list"];
            for (NSDictionary *object in listArray) {
                ListModel *model=[[ListModel alloc]init];
                [model setValuesForKeysWithDictionary:object];
                [self.dataSource addObject:model];
                
            }
        }
        [self.tableView reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
